package com.ylb.rent.consumer;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.ylb.rent.constant.OrderConstant;
import com.ylb.rent.dao.OrderMapper;
import com.ylb.rent.entity.Order;
import com.ylb.rent.service.IOrderService;
import com.ylb.rent.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/7/5 14:25
 */

@Component
@Slf4j
public class OrderDeathLetterConsumer {

    @Autowired
    IOrderService orderService;



    @RabbitListener(queues = "orderCancelDeathQueue")
    public void orderConsumer(String orderNo){

        try {

            Order order = orderService.selectOrderByOrderNo(orderNo);

//        如果order为null
            if(ObjectUtil.isNull(order)){
                log.error("[订单消费者]  取消订单 error 未检测到编号为[{}]的订单",orderNo);
                return;
            }
//            如果order的staus为未支付，则取消订单，否则什么都不用做
            long status = order.getStatus();
            if(status == OrderConstant.STATUS_WAIT){
                boolean b = orderService.payCancel(orderNo);
                log.error("[订单消费者]  取消订单[{}] - {}",orderNo,b);
            }


        }catch (Exception e){
            log.error("[Order-Death-Consumer] MessageBody Parse Order Error");
            e.printStackTrace();
        }

    }
}
